package com.business.serviceimpl.redis;

import com.alibaba.fastjson.JSON;
import com.business.webSocket.WebSocket;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 张子艺
 * @packge com.controller.config.redis
 * @data 2019-01-14 10:12
 * @project Currency
 */
@Service
public class RedisReceiver {

    private static Lock lock  = new ReentrantLock();
    public void oneMessage(String message) {
        System.out.println("data消息通道："+message);
        //群发消息
        for (WebSocket item : WebSocket.webSocketSet) {
            try {
                lock.lock();
                item.sendMessage("本次交易模拟数据为："+message);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
        //这里是收到通道的消息之后执行的方法
    }
    public void twoMessage(String message) {
        System.out.println("moeny消息通道："+message);
        //群发消息
        for (WebSocket item : WebSocket.webSocketSet) {
            try {
                lock.lock();
                item.sendMessage("已产生一笔交易，本次交易模拟金额为：￥ "+message);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
        //这里是收到通道的消息之后执行的方法
    }
    public void endMessage(String message) {
        System.out.println("结束通道："+message);
        //群发消息
        for (WebSocket item : WebSocket.webSocketSet) {
            try {
                lock.lock();
                item.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }
}
